package ysq.xyg.shop.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.EasyPoiUtil;
import ysq.xyg.shop.model.po.ShopOrderCoupon;
import ysq.xyg.shop.model.vo.ShopOrderCouponSExportVo;
import ysq.xyg.shop.model.vo.ShopOrderCouponTExportVo;
import ysq.xyg.shop.model.vo.ShopOrderCouponVo;
import ysq.xyg.shop.service.ShopOrderCouponService;
import ysq.xyg.web.model.po.WebAccount;
import ysq.xyg.web.service.WebMemberService;
import ysq.xyg.wxapi.api.WxMemoryCacheClient;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 商城模块-商品劵管理controller层
 *
 * @author congcong tao
 * @date 2019-4-28 11:19:33
 */
@RestController
public class ShopOrderCouponController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(ShopOrderCouponController.class);
    @Resource
    private ShopOrderCouponService shopOrderCouponService;
    @Resource
    private WebMemberService webMemberService;

    private ShopOrderCoupon shopOrderCoupon;

    /**
     * 分页查询
     *
     * @param shopOrderCouponVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author congcong tao
     * @date 2019-4-28 11:19:33
     */
    @BaseLog(module = "商城模块",className = "商品劵管理",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/shopOrderCoupon/page",method = RequestMethod.POST)
    public ReturnEntity page(ShopOrderCouponVo shopOrderCouponVo, int pageSize, int pageNum){
        if (StringUtils.isNotEmpty(shopOrderCouponVo.getSettleStatus())) {
            shopOrderCouponVo.setStatus("");
        }
        shopOrderCouponVo.setAdminStatus("1");
        ReturnEntity returnEntity = shopOrderCouponService.pageLeft(shopOrderCouponVo,pageSize,pageNum);
        return returnEntity;
    }

    @RequestMapping(value = "/shopOrderCoupon/pageShowOrder",method = RequestMethod.POST)
    public ReturnEntity pageShowOrder(ShopOrderCouponVo shopOrderCouponVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = shopOrderCouponService.pageShowOrder(shopOrderCouponVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-4-28 11:19:33
     */
    @RequestMapping(value = "/shopOrderCoupon/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String id){
        ModelAndView view = new ModelAndView("/shop/shopOrderCouponUpdateOrSave");
        if(StrUtil.isNotEmpty(id)){
            shopOrderCoupon = shopOrderCouponService.selectByPrimaryKey(id);
            view.addObject("shopOrderCoupon", shopOrderCoupon);
        }
        return view;
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-4-28 11:19:33
     */
    @BaseLog(module = "商城模块",className = "商品劵管理",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/shopOrderCoupon/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        shopOrderCouponService.deleteByPrimaryKey(id);
        return new ReturnEntity();
    }

    /**
     * 结算
     * @param id
     * @param status
     * @author congcong tao
     * @date 2019/6/11 9:27
     */
    @RequestMapping(value = "/shopOrderCoupon/updateStatusById")
    public ReturnEntity updateStatusById(String id, String status){
        // 结算
        return shopOrderCouponService.updateStatusById(id, status);
    }

    /**
     * 根据ids结算
     * @param ids
     * @param status
     * @author congcong tao
     * @date 2019/6/20 14:11
     */
    @RequestMapping(value = "/shopOrderCoupon/updateStatusByIds")
    public ReturnEntity updateStatusByIds(String ids, String status){
        // 结算
        return shopOrderCouponService.updateStatusByIds(ids, status);
    }

    /**
     * 处理纠纷
     * 纠纷有效，扣除金额
     * @param id
     * @param issueStatus
     * @param issueMessage
     * @author congcong tao
     * @date 2019/6/11 9:17
     */
    @RequestMapping(value = "/shopOrderCoupon/updateIssueStatusById")
    public ReturnEntity updateIssueStatusById(String id, String issueStatus, String issueMessage, String issueResultMessage){
        // 处理纠纷
        return shopOrderCouponService.updateIssueStatusById(id, issueStatus, issueMessage, issueResultMessage);
    }

    /**
     * 付现
     */
    @RequestMapping(value = "/shopOrderCoupon/updateCashStatus")
    public ReturnEntity updateCashStatus(String orderId, String memberIds, String cashStatus, String cashMessage){
        // 处理
        return shopOrderCouponService.updateCashStatus(orderId, memberIds, cashStatus, cashMessage);
    }

    /**
     * 打款，生成order订单，并将原orderCoupon修改字段
     */
    @RequestMapping(value = "/shopOrderCoupon/createOrder")
    public ReturnEntity createOrder(HttpServletRequest request, String ids, String description){
        return shopOrderCouponService.createOrder(ids, description, acceptCurrentUser(request));
    }

    /**
     * 入库
     */
    @RequestMapping(value = "/shopOrderCoupon/stockIn")
    public ReturnEntity stockIn(HttpServletRequest request, String ids){
        return shopOrderCouponService.stockIn(ids);
    }

    /**
     * 全部入库
     */
    @RequestMapping(value = "/shopOrderCoupon/allStockIn")
    public ReturnEntity allStockIn(HttpServletRequest request){
        return shopOrderCouponService.allStockIn();
    }

    /**
     * 添加或修改
     *
     * @param shopOrderCouponVo Vo对象
     * @author congcong tao
     * @date 2019-4-28 11:19:33
     */
    @BaseLog(module = "商城模块",className = "商品劵管理",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/shopOrderCoupon/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,ShopOrderCouponVo shopOrderCouponVo){
        return shopOrderCouponService.addOrUpdate(shopOrderCouponVo,acceptCurrentUser(request));
    }

    /**
     * 根据下单id查询
     * @param orderId
     * @author congcong tao
     * @date 2019-06-16 20:36
     */
    @RequestMapping(value = "/shopOrderCoupon/shopOrderCouponShowList",method = RequestMethod.GET)
    public ModelAndView shopOrderCouponShowList(String orderId){
        ModelAndView view = new ModelAndView("/shop/shopOrderCouponShowList");
        view.addObject("orderId", orderId);
        return view;
    }

    /**
     * 商品卷导出
     * @param request
     * @param response
     * @param shopOrderCouponVo
     * @author congcong tao
     * @date 2019/6/17 8:51
     */
    @RequestMapping("/shopOrderCoupon/export")
    public void export(HttpServletRequest request, HttpServletResponse response, ShopOrderCouponVo shopOrderCouponVo){
        if (StringUtils.isNotEmpty(shopOrderCouponVo.getSettleStatus())) {
            shopOrderCouponVo.setStatus("");
        }
        shopOrderCouponVo.setAdminStatus("1");
        shopOrderCouponVo.setOrderFlag("2");
        // 根据用户
        List<ShopOrderCoupon> list = shopOrderCouponService.selectEntityList(shopOrderCouponVo);
        List<ShopOrderCouponTExportVo> shopOrderCouponList = new ArrayList<ShopOrderCouponTExportVo>();
        WebAccount wxAccountInfo= WxMemoryCacheClient.getSingleWxAccountInfo();
        String baseUrl = wxAccountInfo.getBaseUrl();
        if (list != null && list.size() > 0) {
            for (ShopOrderCoupon entity : list) {
                ShopOrderCouponTExportVo shopOrderCouponTExportVo = new ShopOrderCouponTExportVo();
                // String nickName = BaseUtil.decodeBase64(shopBusiness.getMemberNickName());
                BeanUtil.copyProperties(entity, shopOrderCouponTExportVo);
                // 需要网络路径
                // String realPath = request.getSession().getServletContext().getRealPath("");
                shopOrderCouponTExportVo.setImage(baseUrl + entity.getImage());
                //shopBusinessExportVo.setMemberNickName(nickName);
                // bean复制
                shopOrderCouponList.add(shopOrderCouponTExportVo);
            }
        }
        String nowDate = DateUtil.formatDate(DateUtil.date());
        // 导出操作
        EasyPoiUtil.exportExcel(shopOrderCouponList, nowDate + "商品卷展示", "商品卷", ShopOrderCouponTExportVo.class, nowDate + "商品卷展示.xls", response);
    }

    /**
     * 打款导出
     * @param request
     * @param response
     * @param shopOrderCouponVo
     * @author congcong tao
     * @date 2019/6/17 8:53
     */
    @RequestMapping("/shopOrderCoupon/exportSettle")
    public void exportSettle(HttpServletRequest request, HttpServletResponse response, ShopOrderCouponVo shopOrderCouponVo){
        if (StringUtils.isNotEmpty(shopOrderCouponVo.getSettleStatus())) {
            shopOrderCouponVo.setStatus("");
        }
        shopOrderCouponVo.setAdminStatus("1");
        shopOrderCouponVo.setOrderFlag("2");
        // 根据用户
        List<ShopOrderCoupon> list = shopOrderCouponService.selectEntityList(shopOrderCouponVo);
        List<ShopOrderCouponSExportVo> shopOrderCouponList = new ArrayList<ShopOrderCouponSExportVo>();
        WebAccount wxAccountInfo= WxMemoryCacheClient.getSingleWxAccountInfo();
        String baseUrl = wxAccountInfo.getBaseUrl();
        if (list != null && list.size() > 0) {
            for (ShopOrderCoupon entity : list) {
                ShopOrderCouponSExportVo shopOrderCouponTExportVo = new ShopOrderCouponSExportVo();
                // String nickName = BaseUtil.decodeBase64(shopBusiness.getMemberNickName());
                BeanUtil.copyProperties(entity, shopOrderCouponTExportVo);
                // 需要网络路径
                // String realPath = request.getSession().getServletContext().getRealPath("");
                shopOrderCouponTExportVo.setImage(baseUrl + entity.getImage());
                // shopBusinessExportVo.setMemberNickName(nickName);
                // bean复制
                shopOrderCouponList.add(shopOrderCouponTExportVo);
            }
        }
        String nowDate = DateUtil.formatDate(DateUtil.date());
        // 导出操作
        EasyPoiUtil.exportExcel(shopOrderCouponList, nowDate + "商品结算展示", "商品商品结算", ShopOrderCouponSExportVo.class, nowDate + "商品结算展示.xls", response);
    }
}